﻿Imports System.Reflection
Imports Microsoft.Office.Interop
Imports System.Timers



Public Class Form1
  Public oApp As Outlook.Application = New Outlook.Application
  Public oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
  Public SAPI = CreateObject("SAPI.spvoice")
  Public oSelectFolders As Outlook.MAPIFolder()
  Public Alert As AlertDialogue = New AlertDialogue

  ' Hide the main form
  Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
    If Not Me.IsHandleCreated Then
      Me.CreateHandle()
      value = False
    End If
    MyBase.SetVisibleCore(value)
  End Sub
#Region "tray item"
  Friend traySettings As Form2
  Private WithEvents trayItem As NotifyIcon
  Private WithEvents formMain As ContextMenuStrip
  Private WithEvents formMenu As ToolStripMenuItem
  Private WithEvents formDiv As ToolStripSeparator
  Private WithEvents formExit As ToolStripMenuItem

  Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Dim trayIcon As New Bitmap("C:\Users\jpa0063\Desktop\SoftDev\Project1\EmailCheckFacInabox\EmailCheck\eo.bmp")
    Dim HIcon As IntPtr = trayIcon.GetHicon

    'tray menu items
    formMenu = New ToolStripMenuItem("Settings")
    formDiv = New ToolStripSeparator()
    formExit = New ToolStripMenuItem("Exit")
    formMain = New ContextMenuStrip
    formMain.Items.AddRange(New ToolStripItem() {formMenu, formDiv, formExit})

    trayItem = New NotifyIcon
    trayItem.Icon = System.Drawing.Icon.FromHandle(HIcon)
    trayItem.ContextMenuStrip = formMain
    trayItem.Text = "Eagle Outlook"
    trayItem.Visible = True

    traySettings = New Form2
  End Sub

#Region "tray subs"
  'right click on tray icon and select "Settings" to open settings form
  Private Sub formMenu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles formMenu.Click
    If Me.Visible = True Then
      Me.BringToFront()
      Me.Focus()
    Else
      tmrUserCheck.Stop()
      traySettings.ShowDialog()
      tmrUserCheck.Interval = traySettings.alertInterval * 1000
      tmrUserCheck.Start()
    End If
  End Sub
  'Double click tray icon and open settings form
  Private Sub trayItem_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles trayItem.DoubleClick
    If Me.Visible = True Then
      Me.BringToFront()
      Me.Focus()
    Else
      tmrUserCheck.Stop()
      traySettings.ShowDialog()
      tmrUserCheck.Interval = traySettings.alertInterval * 1000
      tmrUserCheck.Start()
    End If
  End Sub
  'right click on tray icon and click "Exit" to shutdown
  Private Sub formExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles formExit.Click
    Application.Exit()
  End Sub
#End Region 'tray subs 
#End Region 'tray item

#Region "others"


  'User Inbox Check
  Private Sub tmrUserCheck_Tick(sender As System.Object, e As System.EventArgs) Handles tmrUserCheck.Tick
    tmrUserCheck.Stop()
    oNS.Logon("Outlook", Missing.Value, False, True) ' TODO:

    Dim nUnreadCt As Integer = 0

    'Get the count of all the messages in the selected folders
    For i As Integer = 0 To (traySettings.checks.Length - 2)
      For j As Integer = 1 To (traySettings.checks(i).Items.Count)
        If traySettings.checks(i).GetItemCheckState(j - 1) Then nUnreadCt += traySettings.oAccounts(i).Folders(j).UnReadItemCount
      Next
    Next

    ' Play an audio and show a visual, if enabled, of the number of new messages
    If traySettings.visual Then
      If nUnreadCt.Equals(1) Then
        Alert.showmessage(nUnreadCt)
        Alert.Show()
      ElseIf nUnreadCt > 1 Then
        Alert.showmessage(nUnreadCt)
        Alert.Show()
      End If
    End If
    If traySettings.audio Then
      If nUnreadCt.Equals(1) Then
        SAPI.Speak("You have " & nUnreadCt & " new message.")
      ElseIf nUnreadCt > 1 Then
        SAPI.Speak("You have " & nUnreadCt & " new messages")
      End If
    End If
   

    ' Log off.
    oNS.Logoff()
    tmrUserCheck.Start()
  End Sub
#End Region
End Class


